package com.ruoyi.shouye.service.impl;

import com.ruoyi.cangku.domain.Cangku;
import com.ruoyi.cangku.service.ICangkuService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.gouhuoruku.domain.GouhuoRuku;
import com.ruoyi.gouhuoruku.service.IGouhuoRukuService;
import com.ruoyi.qianhuankuan.domain.Qianhuankuan;
import com.ruoyi.qianhuankuan.service.IQianhuankuanService;
import com.ruoyi.shouye.domain.CangkuKucunVo;
import com.ruoyi.shouye.domain.GoodNameDto;
import com.ruoyi.shouye.domain.XiaoshouDto;
import com.ruoyi.shouye.domain.XiaoshouVo;
import com.ruoyi.shouye.mapper.ShouyeMapper;
import com.ruoyi.shouye.service.IShouyeService;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import com.ruoyi.xiaoshouchuku.service.IXiaoshouChukuService;
import com.ruoyi.yuangong.domain.Yuangong;
import com.ruoyi.zongdianpifa.service.IZongdianPifaService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
@Slf4j
@AllArgsConstructor
public class ShouyeServiceImpl  implements IShouyeService {

    @Resource
    private ICangkuService cangkuService;
    @Resource
    private IXiaoshouChukuService chukuService;
    @Resource
    private IZongdianPifaService pifaService;
    @Resource
    private IQianhuankuanService qianhuankuanService;
    @Resource
    private IGouhuoRukuService gouhuoRukuService;
    @Resource
    private ShouyeMapper shouyeMapper;

    @Override
    public List<XiaoshouDto> todayData() {
        //查询各仓库的数据
        Cangku cangku = new Cangku();
        List<Cangku> cangkuList = cangkuService.selectCangkuList(cangku);
        List<XiaoshouDto> xiaoshouDtoList = new ArrayList<>();

        //根据仓库数据查询批发总店表的当天数据累加各店金额、客户未还款、采购总金额、利润金额
        for (Cangku ck : cangkuList) {
            XiaoshouDto xiaoshouDto = new XiaoshouDto();
            xiaoshouDto.setJiner(new BigDecimal(0));
            xiaoshouDto.setWeihuankuan(new BigDecimal(0));
            xiaoshouDto.setGouhuozongjiner(new BigDecimal(0));
            xiaoshouDto.setLirunjiner(new BigDecimal(0));
            //仓库
            xiaoshouDto.setChukuCangku(ck.getCangkuName());
            //日期
            xiaoshouDto.setChukuDate(new Date());
            xiaoshouDto.setBeginTime(getStartTime());
            xiaoshouDto.setEndTime(getEndTime());

            //计算各店当日销售金额
            List<XiaoshouChuku> chukuList = shouyeMapper.selectXiaoshouChukuByDate(xiaoshouDto);
            if (chukuList != null){
                for (XiaoshouChuku xsck : chukuList) {
                    xiaoshouDto.setJiner(xsck.getJine().add(xiaoshouDto.getJiner()));
                }
            }
            //客户未还款 ，欠还款表的当日欠款
            List<Qianhuankuan> qianhuankuanList = shouyeMapper.selectQianhuankuanList(xiaoshouDto);
            if (qianhuankuanList != null){
                for (Qianhuankuan qhk : qianhuankuanList) {
                    //未还款金额累加
                    xiaoshouDto.setWeihuankuan(qhk.getZongqiankuanjine().add(xiaoshouDto.getWeihuankuan()));
                }
            }
            //采购总金额 ，累加今日购货入库表金额
            List<GouhuoRuku> gouhuoRukuList = shouyeMapper.selectGouhuoList(xiaoshouDto);
            if (gouhuoRukuList != null){
                for (GouhuoRuku ghrk : gouhuoRukuList) {
                    xiaoshouDto.setGouhuozongjiner(ghrk.getJine().add(xiaoshouDto.getGouhuozongjiner()));
                }
            }
            //利润金额，利润是销售减采购
            //如果销售金额不是空的，且采购金额不是空的
            if (xiaoshouDto.getJiner() != null && xiaoshouDto.getGouhuozongjiner() != null){
                BigDecimal lirun = xiaoshouDto.getJiner().subtract(xiaoshouDto.getGouhuozongjiner());
                xiaoshouDto.setLirunjiner(lirun);
            }
            //销售金额不是空的，但采购金额是空的
            else if (xiaoshouDto.getJiner() != null && xiaoshouDto.getGouhuozongjiner() == null){
                xiaoshouDto.setLirunjiner(xiaoshouDto.getJiner());
            }
            xiaoshouDtoList.add(xiaoshouDto);
        }
        return xiaoshouDtoList;
    }
    private Calendar dayStart(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart;
    }

    private Calendar dayEnd(){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd;
    }

    /**
     * 今天开始时间
     * @return
     */
    private Date getStartTime() {
        return this.dayStart().getTime();
    }

    /**
     * 今天结束时间
     * @return
     */
    private Date getEndTime() {
        return this.dayEnd().getTime();
    }

    /**
     * 所有仓库7天内的销货金额和购货金额
     */
    @Override
    public XiaoshouDto selectXiaoshouChukuByDate() {
        List<XiaoshouDto> xiaoshouDtoList = new ArrayList<>();
        List<BigDecimal> xiaohuoList = new ArrayList<>();
        List<BigDecimal> gouhuoList = new ArrayList<>();

        Calendar dayStart = this.dayStart();
        Calendar dayEnd = this.dayEnd();
        for (int i = 0; i < 7; i++) {
            Date beginTime;
            Date endTime;
            //时间
            if (i==0){
                dayStart.add(Calendar.DATE, -6);
                beginTime = dayStart.getTime();
                dayEnd.add(Calendar.DATE, -6);
                endTime = dayEnd.getTime();
            }else {
                dayStart.add(Calendar.DATE, +1);
                beginTime = dayStart.getTime();
                dayEnd.add(Calendar.DATE, +1);
                endTime = dayEnd.getTime();
            }
            XiaoshouDto xiaoshouDto = new XiaoshouDto();
            xiaoshouDto.setBeginTime(beginTime);
            xiaoshouDto.setEndTime(endTime);
            //销货金额，销售出库表计算
            List<XiaoshouChuku> chukuList = shouyeMapper.selectXiaoshouChukuByDate(xiaoshouDto);
            if (chukuList != null) {
                BigDecimal sumXiaoshouPrice = chukuList.stream().map(XiaoshouChuku::getJine ).reduce(BigDecimal.ZERO, BigDecimal::add);
                xiaohuoList.add(sumXiaoshouPrice);
            }
            //购货金额
            List<GouhuoRuku> gouhuoRukuList = shouyeMapper.selectGouhuoList(xiaoshouDto);
            if (gouhuoRukuList != null) {
                BigDecimal sumGouhuorukuPrice = gouhuoRukuList.stream().map(GouhuoRuku::getJine ).reduce(BigDecimal.ZERO, BigDecimal::add);
                gouhuoList.add(sumGouhuorukuPrice);
            }
            xiaoshouDtoList.add(xiaoshouDto);
        }

        XiaoshouDto dto = new XiaoshouDto();
        dto.setXiaohuoData(xiaohuoList);
        dto.setGouhuoData(gouhuoList);
        return dto;
    }

    /**
     * 各仓库商品库存
     * @return
     */
    public List<CangkuKucunVo> kucunBycangku(){
        //查询出仓库和库存总数
        List<CangkuKucunVo> cangkuKucunDtoList = shouyeMapper.cangkuAndkucun();

        for (CangkuKucunVo ck : cangkuKucunDtoList) {
            //商品总数
            List<GoodNameDto> cangkuKucunVoList = shouyeMapper.goodNameSum(ck.getCangkuName());
            for (GoodNameDto goodNameDto:cangkuKucunVoList) {
                //计算商品总数占比
                Long kuncun = goodNameDto.getKuncun();
                //占比
                Long d = 0L;
                if(ck.getKuchunAll()>0) {
                    d = kuncun / ck.getKuchunAll();
                }
                goodNameDto.setZhanbi(d+"%");
            }
            ck.setKuchunGoodName(cangkuKucunVoList);
        }
        return cangkuKucunDtoList;
    }

    /**
     * 当前登陆人所在仓库今日销量排行前10
     * @return
     */
    @Override
    public List<XiaoshouVo> selectXiaoshouChukupaihangByShuliang() {
        // 获取当前的用户名或者id查询SysUser表获取nickName，用户昵nickName称代表仓库
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        String changkuName = loginUser.getNickName();
//        List<XiaoshouVo> xiaoshouVoList = shouyeMapper.selectXiaoshouChukupaihangByShuliang(changkuName,getStartTime(),getEndTime());
        List<XiaoshouVo> xiaoshouVoList = shouyeMapper.selectXiaoshouChukupaihangByShuliang(getStartTime(),getEndTime());
        return xiaoshouVoList;
    }

    /**
     * 当前登陆人所在仓库今日出勤情况
     * @return
     */
    @Override
    public List<Yuangong> selectTodayYuangongList() {
        List<Yuangong> yuangongList = shouyeMapper.selectTodayYuangongList(SecurityUtils.getLoginUser().getUser().getNickName(),getStartTime(),getEndTime());
        return yuangongList;
    }
}
